$(function () {
    console.log(authorities);
    let pageNum = 0
//用户信息显示
    $("#user_in_log").hide();
    $("#user_un_log").show();
    if(user_data.name != undefined){
        $("#user_un_log").hide();
        $("#user_in_log").show();
        $("#user_main_id").text('' + user_data.name + '');
        $("#top_user_main_id").text('' + user_data.name + '');
        $("#top_main_user_img").attr('src', '' + getAliossPath("user/" + user_data.id, user_data.headimg) + '');
        $("#user_main_img").attr('src', '' + getAliossPath("user/" + user_data.id, user_data.headimg) + '');
        $("#user_main_img").attr('user_id', user_data.id);
        $("#user_main_img").click(function () {
            $.session.set('user_info_user_id', ''+user_data.id+'')
        });
    }

    //获取频道
    $.ajax({
        url: baseURL + '/webapi/channel/searchPage',
        type: 'post',
        dataType: 'json',
        headers: {'authorities': authorities},
        data: {
            pid: 0,
            nodeType: 0,
            pageNum: 1,
            pageSize: 100,
        },
        async: false,//同步
        success: function (data) {
            if (data.code == 1) {
                //for i 循环
                for (let e = 0; e < 11; e++) {
                    $("#list-item0").before(
                        '<a uid="' + data.data[e].id + '" style="display: inline-block" class="channel-item list-group-item">' + data.data[e].name + '</a>'
                    );
                }
                //for i 循环
                for (let i = 11; i < data.data.length; i++) {
                    $("#more_item").append(
                        '<a uid="' + data.data[i].id + '" style="display: inline-block" class="channel-item list-group-item more_item">' + data.data[i].name + '</a>'
                    );
                }
            } else if (data.code == "401") {
                alert("账号在别处登录，请重新登录")
                window.location.href = "login.html"
            } else {
                alert(data.msg)
            }
        }
    })
//更多频道
    $(".more_item_button, .more_item").hover(function () {
        $(".more_item").show();
    }, function () {
        $(".more_item").hide();
    });
    // $(".more_item").mouseout();
//置顶的内容
    $.ajax({
        url: baseURL + '/webapi/works/searchForcePropWorksPage',
        type: 'post',
        dataType: 'json',
        headers: {'authorities': authorities},
        data: {
            prop:5,
            pageNum: 0,
            pageSize: 10,
        },
        async: false,//同步
        success: function (data) {
            for (let i = 0; i < data.data.length; i++) {
                let content_type = data.data[i].type
                content(content_type, data.data[i])
            }
        }

    })
//切换频道推送内容
    let channel = 1
    $.session.set('channel', channel)
    $(".dw").on('click', '.channel-item', function () {
        $(".list-group-item").removeClass('active');
        $(this).addClass('active');
        channel = $(this).attr('uid');
        console.log(channel);
        $.session.set('channel', channel)
        let cccccc = $.session.get('channel')
        console.log(cccccc);
        $("#main_content").empty();
        change_channel(channel)
    });


    function change_channel(channel) {
        $.ajax({
            url: baseURL + '/webapi/works/searchChannelsAllPage',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                prop: 0,
                rotation: 0,
                pageNum: pageNum,
                pageSize: 10,
                channel: channel,
            },
            async: false,//同步
            success: function (data) {
                $("#main_content").empty();
                //生成内容列表
                for (let i = 0; i < data.data.length; i++) {
                    let content_type = data.data[i].type
                    content(content_type, data.data[i])
                }
            }
        })
    }

//进入时获取推荐内容
    $("#list-item1").click(function () {
        location.reload();
    });

    function main_index() {
        pageNum++
        $.ajax({
            url: baseURL + '/webapi/works/searchRecommend',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                prop: -1,
                rotation: -1,
                pageNum: pageNum,
                pageSize: 30,
            },
            async: false,//同步
            success: function (data) {
                if(data.code != 1){
                    // alert(data.msg,)
                }else {
                    console.log(data);
                    if (data.data.length != 1) {
                        //生成内容列表
                        for (let i = 0; i < data.data.length; i++) {
                            let content_type = data.data[i].type
                            content(content_type, data.data[i])
                        }
                    } else if (data.total == 1) {
                        return
                    }
                }
            }

        })
    }

    dayhot()
    main_index()
//触底检测
    $(document).scroll(function () {
        if (channel == 1) {
            if ($(document).height() - $(window).height() <= $(window).scrollTop() + 500) {
                main_index()
            }
        } else {
            change_channel(channel)
        }


    });

//网址导航列表

    function navigation() {
        $.ajax({
            url: baseURL + '/webapi/website/searchWebsites',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                type: 0,
                prop: 0,
            },
            async: false,//同步
            success: function (data) {
                for (let i = 0; i < data.data.length; i++) {
                    $(".about_more").append(
                        '<div class="col-sm-4 sode_bar_main_content_navigation"><a href="' + data.data[i].url + '" uid="' + data.data[i].id + '">' + data.data[i].name + '</a></div>\n'
                    );
                }
            }

        })
    }

    navigation()

//首页信息流方法
    function content(content_type, data) {
        // 判断内容类型
        if (content_type == 'headlines') {
            let img_url = getPicUrl(getAliossPath("work/" + data.id, data.tpic), 500, 0)
            if (img_url == 'images/index/视角LOGO 背景.jpg?x-oss-process=image/resize,w_500') {
                $("#main_content").append(
                    '            <div class="post-list-item col-sm-12" style="padding-left: 0;" uid="' + data.id + '">\n' +
                    '                <div class="" style="margin-top: 5px;display: inline-block;margin-left: 15px">\n' +
                    '                    <a >\n' +
                    '                        <div class="post-heading link" type="'+data.type+'" uid="' + data.id + '">' + data.title + '</div>\n' +
                    '                    </a>\n' +
                    '                    <p class="">\n' +
                    '                        <a  class="user_link" user_id="'+data.updatedBy+'"><span>\n' +
                    '                            <img user_id="' + data.updatedBy+'"class="avatar user_link"\n' +
                    '                                 src="' + getAliossPath("user/" + data.updatedBy, data.headImg) + '">\n' +
                    '                        ' + data.uname + '\n' +
                    '                        </span> ⋅</a>\n' +
                    '                        <span>\n' +
                    '                        ' + data.commentCount + '评论\n' +
                    '                        </span> ⋅\n' +
                    '                        <span>\n' +
                    '                                ' + time(data.atime) + '\n' +
                    '                            </span>\n' +
                    '                    </p>\n' +
                    '                </div>\n' +
                    '            </div>'
                );
            } else {
                $("#main_content").append(
                    '            <div class="post-list-item col-sm-12"style="padding-left: 0;" uid="' + data.id + '">\n' +
                    '                <div class="head_img fl ">\n' +
                    '                    <img class="title_head_img link" type="'+data.type+'" uid="'+data.id+'" src="' + getPicUrl(getAliossPath("work/" + data.id, data.tpic), 500, 0) + '" alt="">\n' +
                    '                </div>\n' +
                    '                <div class="" style="margin-top: 5px;display: inline-block;margin-left: 0">\n' +
                    '                    <a >\n' +
                    '                        <div class="post-heading link" type="'+data.type+'" uid="' + data.id + '">' + data.title + '</div>\n' +
                    '                    </a>\n' +
                    '                    <p class="">\n' +
                    '                        <a class="user_link" user_id="'+data.updatedBy+'"><span>\n' +
                    '                            <img class="avatar user_link" user_id="'+data.updatedBy+'"\n' +
                    '                                 src="' + getAliossPath("user/" + data.updatedBy, data.headImg) + '">\n' +
                    '                        ' + data.uname + '\n' +
                    '                        </span> ⋅</a>\n' +
                    '                        <span>\n' +
                    '           ' +
                    '             ' + data.commentCount + '评论\n' +
                    '                        </span> ⋅\n' +
                    '                        <span>\n' +
                    '                                ' + time(data.atime) + '\n' +
                    '                            </span>\n' +
                    '                    </p>\n' +
                    '                </div>\n' +
                    '            </div>'
                );
            }

        } else if (content_type == 'article') {
            $("#main_content").append(
                '<div class="no-mode col-sm-12" uid="' + data.id + '">\n' +
                '    <div class="title-box col-sm-12"><a class="link"  type="'+data.type+'"  uid="' + data.id + '">' + data.title + '</a></div>\n' +
                '    <div class="footer-bar col-sm-12">\n' +
                '        <div class="footer-bar-left col-sm-10" style="padding-left: 0;">\n' +
                '            <a class="footer-bar-action tag tag-style-video" >微头条</a>\n' +
                '            <a  user_id="'+data.updatedBy+'" class="footer-bar-action media-avatar user_link" ><img\n' +
                '                    src="' + getAliossPath("user/" + data.updatedBy, data.headImg) + '" alt=""></a>\n' +
                '            <a class="footer-bar-action source user_link" user_id="'+data.updatedBy+'"> ' + data.uname + '·&nbsp; </a>\n' +
                '            <a class="footer-bar-action source" > ' + data.commentCount + '评论·&nbsp;</a>\n' +
                '            <span class="footer-bar-action source time">' + time(data.atime) + '</span>\n' +
                '        </div>\n' +
                '        <div class="footer-bar-right col-sm-2"></div>\n' +
                '    </div>\n' +
                '</div>'
            );
        } else if (content_type == 'imgtext') {
            $("#main_content").append(
                '<div class="ugc-mode col-sm-12" type="'+data.type+'" uid="' + data.id + '" style="z-index: 1">\n' +
                '    <div class="col-xs-3 ugc-mode-lbox">\n' +
                '        <a class="img-wrap" >\n' +
                '            <img type="'+data.type+'" uid="' + data.id + '" class="post-object lazy-load-img link"\n' +
                '                 src="' + getAliossPath("work/" + data.id, img_symbol(data.tpic)) + '">\n' +
                '        </a>\n' +
                '    </div>\n' +
                '    <div class="col-sm-9 ugc-mode-right ugc-mode-rbox">\n' +
                '        <div class="col-sm-12 ugc-mode-rbox-inner" uid="'+data.id+'" type ='+data.type+' style="margin-top: 5px">\n' +
                '            <div class="ugc-mode-user">\n' +
                '                <a><img user_id="'+data.updatedBy+'" class="ugc-avatar user_link" src="' + getAliossPath("user/" + data.updatedBy, data.headImg) + '" alt=""></a>\n' +
                '                <div class="ugc-desc">\n' +
                '                    <a class="ugc-name" >\n' +
                '                        <span class="user_link" user_id="'+data.updatedBy+'">' + data.uname + '</span>\n' +
                '                        <span class="dv-icon" style=";"></span>\n' +
                '                        <i class="bui-icon icon-vtt"></i>\n' +
                '                    </a>\n' +
                '                    <p class="ugc-meta">\n' +
                '                        <span>' + data.signature + '</span>\n' +
                '                    </p>\n' +
                '                </div>\n' +
                '            </div>\n' +
                '            <div class="ugc-mode-content">\n' +
                '                <a  class="link" type="'+data.type+'" uid="' + data.id + '">' + data.title + '</a>\n' +
                '            </div>\n' +
                '            <div class="ugc-mode-footer">\n' +
                '                <div class="footer-bar-left">\n' +
                '                    <a class="footer-bar-action source user_link" user_id="'+data.updatedBy+'"> ' + data.uname + '·&nbsp; </a>\n' +
                '                    <a class="footer-bar-action source" > ' + data.commentCount + '评论·&nbsp;</a>\n' +
                '                    <span class="footer-bar-action source time">' + time(data.atime) + '</span>\n' +
                '                </div>\n' +
                '                <div class="footer-bar-right">\n' +
                '\n' +
                '                </div>\n' +
                '            </div>\n' +
                '        </div>\n' +
                '    </div>\n' +
                '</div>\n'
            );
        } else if (content_type == 'lvideo') {
            if(data.tpic == null || data.tpic == ""){
                $("#main_content").append(
                    '            <div class="post-list-item col-sm-12"style="padding-left: 0;"  uid="' + data.id + '">\n' +
                    '                <div class="" style="margin-top: 5px;display: inline-block;margin-left: 30px">\n' +
                    '                    <a >\n' +
                    '                        <div class="post-heading link" type="'+data.type+'" uid="' + data.id + '">' + data.title + '</div>\n' +
                    '                    </a>\n' +
                    '                    <p class="">\n' +
                    '                        <a class="user_link" user_id="'+data.updatedBy+'" ><span>\n' +
                    '                            <img type="'+data.type+'" uid="' + data.id + '" class="avatar link"\n' +
                    '                                 src="' + getAliossPath("user/" + data.updatedBy, data.headImg) + '">\n' +
                    '                        ' + data.uname + '\n' +
                    '                        </span> ⋅</a>\n' +
                    '                        <span>\n' +
                    '                        ' + data.commentCount + '评论\n' +
                    '                        </span> ⋅\n' +
                    '                        <span>\n' +
                    '                                ' + time(data.atime) + '\n' +
                    '                            </span>\n' +
                    '                    </p>\n' +
                    '                </div>\n' +
                    '            </div>'
                );
            }else {
                $("#main_content").append(
                    '            <div class="post-list-item col-sm-12"style="padding-left: 0;" type="'+data.type+'" uid="' + data.id + '">\n' +
                    '                <div class="head_img fl ">\n' +
                    '                    <img class="title_head_img link" type="'+data.type+'" uid="'+data.id+'" src="' + getPicUrl(getAliossPath("work/" + data.id, data.tpic), 500, 0) + '" alt="">\n' +
                    '                </div>\n' +
                    '                <div class="" style="margin-top: 5px;display: inline-block;margin-left: 0">\n' +
                    '                    <a >\n' +
                    '                        <div class="post-heading link" type="'+data.type+'" uid="' + data.id + '">' + data.title + '</div>\n' +
                    '                    </a>\n' +
                    '                    <p class="">\n' +
                    '                        <a class="user_link" user_id="'+data.updatedBy+'"><span>\n' +
                    '                            <img type="'+data.type+'" uid="' + data.id + '" class="avatar link"\n' +
                    '                                 src="' + getAliossPath("user/" + data.updatedBy, data.headImg) + '">\n' +
                    '                        ' + data.uname + '\n' +
                    '                        </span> ⋅</a>\n' +
                    '                        <span>\n' +
                    '                        ' + data.commentCount + '评论\n' +
                    '                        </span> ⋅\n' +
                    '                        <span>\n' +
                    '                                ' + time(data.atime) + '\n' +
                    '                            </span>\n' +
                    '                    </p>\n' +
                    '                </div>\n' +
                    '            </div>'
                );
            }


        } else if (content_type == 'svideo') {
            $("#main_content").append(
                '            <div class="post-list-item col-sm-12"style="padding-left: 0;" type="'+data.type+'" uid="' + data.id + '">\n' +
                '                <div class="head_img fl ">\n' +
                '                    <img class="title_head_img link" type="'+data.type+'" uid="' + data.id + '" src="' + getPicUrl(getAliossPath("work/" + data.id, data.tpic), 500, 0) + '" alt="">\n' +
                '                </div>\n' +
                '                <div class="" style="margin-top: 5px;display: inline-block;margin-left: 0">\n' +
                '                    <a>\n' +
                '                        <div class="post-heading link" type="'+data.type+'" uid="' + data.id + '">' + data.title + '</div>\n' +
                '                    </a>\n' +
                '                    <p class="">\n' +
                '                        <a class="user_link" user_id="'+data.updatedBy+'"><span>\n' +
                '                            <img user_id="'+data.updatedBy+'" type="'+data.type+'" uid="' + data.id + '" class="avatar user_link"\n' +
                '                                 src="' + getAliossPath("user/" + data.updatedBy, data.headImg) + '">\n' +
                '                        ' + data.uname + '\n' +
                '                        </span> ⋅</a>\n' +
                '                        <span>\n' +
                '                        ' + data.commentCount + '评论\n' +
                '                        </span> ⋅\n' +
                '                        <span>\n' +
                '                                ' + time(data.atime) + '\n' +
                '                            </span>\n' +
                '                    </p>\n' +
                '                </div>\n' +
                '            </div>'
            );
        } else if (content_type == 'news') {
            $("#main_content").append(
                '<div class="no-mode col-sm-12" uid="' + data.id + '">\n' +
                '    <div class="title-box col-sm-12"><a class="link" type="news" uid="' + data.id + '">' + data.title + '</a></div>\n' +
                '    <div class="footer-bar col-sm-12">\n' +
                '        <div class="footer-bar-left col-sm-10" style="padding-left: 0;">\n' +
                '            <a class="footer-bar-action tag tag-style-video" >新闻</a>\n' +
                '            <span class="footer-bar-action source" > ' + data.author + '·&nbsp; </span>\n' +
                // '            <a class="footer-bar-action source" > ' + data.commentCount + '评论·&nbsp;</a>\n' +
                '            <span class="footer-bar-action source time">' + time(data.atime) + '</span>\n' +
                '        </div>\n' +
                '        <div class="footer-bar-right col-sm-2"></div>\n' +
                '    </div>\n' +
                '</div>'
            );
        }

    }

//24小时热闻方法
    function dayhot() {
        $.ajax({
            url: baseURL + '/webapi/works/searchRecommend',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                prop: -1,
                rotation: -1,
                pageNum: 1,
                pageSize: 3,
            },
            async: false,//同步
            success: function (data) {
                console.log(data);
                //for i 循环
                for (let i = 0; i < 3; i++) {
                    if(data.data[i].tpic == null || data.data[i].tpic == ""){
                        $(".article-list").append(
                            '<div class="hot_news_item">\n' +
                            '    <li class="article-item">\n' +
                            '         <a class="news-link">\n' +
                            // '            <div class="news-pic"><img class="link" type="'+data.data[i].type+'" uid="' + data.data[i].id + '" src="' + getPicUrl(getAliossPath("user/" + data.data[i].createdBy,img_symbol(data.data[i].tpic)), 500, 0) + '" alt=""></div>\n' +
                            '            <div class="news-inner" style="display: flex;align-items: center;"><p class="title link" type="' + data.data[i].type + '" uid="' + data.data[i].id + '" style="max-height: 60px;overflow : hidden;vertical-align:middle;">' + data.data[i].title + '</p></div>\n' +
                            '        </a>\n' +
                            '    </li>\n' +
                            '</div>\n'
                        );
                    }else {
                        if(data.data[i].type != 'news'){
                            $(".article-list").append(
                                '<div class="hot_news_item">\n' +
                                '    <li class="article-item">\n' +
                                '         <a class="news-link">\n' +
                                '            <div class="news-pic"><img style="object-fit:cover;" class="link" type="'+data.data[i].type+'" uid="' + data.data[i].id + '" src="' + getPicUrl(getAliossPath("work/" + data.data[i].id,data.data[i].tpic), 500, 0) + '" alt=""></div>\n' +
                                '            <div class="news-inner"><p class="title link" type="' + data.data[i].type + '" uid="' + data.data[i].id + '" style="max-height: 60px;overflow : hidden;">' + data.data[i].title + '</p></div>\n' +
                                '        </a>\n' +
                                '    </li>\n' +
                                '</div>\n'
                            );
                        }else {
                            $(".article-list").append(
                                '<div class="hot_news_item">\n' +
                                '    <li class="article-item">\n' +
                                '         <a class="news-link">\n' +
                                '            <div class="news-pic"><img style="object-fit:cover;" class="link" type="'+data.data[i].type+'" uid="' + data.data[i].id + '" src="' + getPicUrl(getAliossPath("work/" +data.data[i].tpic), 500, 0) + '" alt=""></div>\n' +
                                '            <div class="news-inner"><p class="title link" type="' + data.data[i].type + '" uid="' + data.data[i].id + '" style="max-height: 60px;overflow : hidden;">' + data.data[i].title + '</p></div>\n' +
                                '        </a>\n' +
                                '    </li>\n' +
                                '</div>\n'
                            );
                        }

                    }

                }
            }

        })

    }

    //图片分割
    function img_symbol(img_data) {
        let arr = (img_data || "").split(',');
        return arr[0]
    }

//时间截取方法
    function time(time) {
        arr = (time || "").split('T');
        return arr[0]
    }
})